# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=oneTBB
pkgbase=mingw-w64-tbb
pkgname=("${MINGW_PACKAGE_PREFIX}-tbb")
pkgver=2021.10.0
pkgrel=1
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://software.intel.com/en-us/oneapi/onetbb'
license=('spdx:Apache-2.0')
pkgdesc='oneAPI Threading Building Blocks (mingw-w64)'
conflicts=("${MINGW_PACKAGE_PREFIX}-intel-tbb")
replaces=("${MINGW_PACKAGE_PREFIX}-intel-tbb")
provides=("${MINGW_PACKAGE_PREFIX}-intel-tbb")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-hwloc")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/oneapi-src/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        001-fix-using-TBB-with-Debug-build-type.patch
        002-fix-link-flags-on-mingw.patch
        004-support-mingw-aarch64.patch)
sha256sums=('487023a955e5a3cc6d3a0d5f89179f9b6c0ae7222613a7185b0227ba0c83700b'
            '094c556a1087563a3a5810db0fa81aed6432b3a8e91aab9a747e9a55ea262f2a'
            'dd34fd7f652d3da9dd94c343f46787a2b9e631fb7ec01033f51f96dc6319c5b4'
            '0f28cdc8a90e8dc3870cd584cf9f5f683e0a35696fe4dd816d1f6b8b5ea39de5')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-fix-using-TBB-with-Debug-build-type.patch
  patch -p1 -i ${srcdir}/002-fix-link-flags-on-mingw.patch
  patch -p1 -i ${srcdir}/004-support-mingw-aarch64.patch
  find . -name "*.orig" -exec rm -f {} \;
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  if check_option "debug" "n"; then
    _build_type="Release"
  else
    _build_type="Debug"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_BUILD_TYPE=${_build_type} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DTBB_TEST=OFF \
    -DTBB_STRICT=OFF \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install build-${MSYSTEM}
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
